﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.TrainingUI
{
    public partial class AddTraining : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetTrainings();
            }

        }
        protected void SaveTraining(object o, EventArgs e)
        {
            try
            {
                Training training = new Training();
                TrainingManager manager = new TrainingManager();
                training.Name = txtTraingName.Value;
                training.Department = txtDepartment.Value;
                training.Trainer = txtTrainer.Value;
                training.Topics = txtTopice.Value;
                training.Venue = txtVenue.Value;
                training.StartDate = txtStartDate.Value;
                training.EndDate = txtEndDate.Value;
                training.Description = txtDescription.Value;
                if (manager.SaveTraining(training))
                {
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "Training Successfully Saved";
                    GetTrainings();
                }
            }
            catch (Exception ex)
            {
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "Training Not Saved";
            }
        }

        protected void GetTrainings()
        {
            List<Training> trainings = new List<Training>();
            TrainingManager trainingManager = new TrainingManager();
            trainings = trainingManager.GetAllTrainings();
            allTrainingGdv.DataSource = trainings;
            allTrainingGdv.DataBind();

        }

        protected void allTrainingGdv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            allTrainingGdv.EditIndex = -1;
            GetTrainings();
        }

        protected void allTrainingGdv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            allTrainingGdv.EditIndex = e.NewEditIndex;
            GetTrainings();
        }

        protected void allTrainingGdv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                TrainingManager trainingManager = new TrainingManager();
                Training aTraining = new Training();
                aTraining.Id = Convert.ToInt32(((Label)allTrainingGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                aTraining.Name = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtTrainingName")).Text;
                aTraining.Department = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtDepartmentname")).Text;
                aTraining.Trainer = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtTrainerName")).Text;
                aTraining.Topics = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtTopics")).Text;
                aTraining.Venue = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtVenueName")).Text;
                aTraining.StartDate = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtStartDate")).Text;
                aTraining.EndDate = ((TextBox)allTrainingGdv.Rows[e.RowIndex].FindControl("txtEndDate")).Text;

                if (trainingManager.UpdateTrainingInfo(aTraining.Id, aTraining))
                {
                    lblMsg.Text = "Training Information Successfully Updated.";
                    lblMsg.ForeColor = Color.Green;
                    allTrainingGdv.EditIndex = -1;
                    GetTrainings();
                }
                else
                {
                    lblMsg.Text = "Sorry! Training Information not Updated.";
                    lblMsg.ForeColor = Color.Red;
                }


            }
            catch (Exception ex)
            {

                lblMsg.Text = ex.Message;
                lblMsg.ForeColor = Color.Red;
            }



        }

        protected void allTrainingGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                PunishedProcedureManager punishedProcedure = new PunishedProcedureManager();
                int id = Convert.ToInt32(((Label)allTrainingGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                string msg = punishedProcedure.DeleteProcedure(id);
                if (msg.Contains("Successfully"))
                {
                    lblDelete.Text = "Punished Procedure Successfully deleted";
                    lblDelete.ForeColor = Color.Green;
                    GetTrainings();

                }
                else
                {
                    lblDelete.Text = "sorry !Punished Procedure not deleted";
                    lblDelete.ForeColor = Color.Red;
                }

            }
            catch (Exception)
            {
                lblDelete.Text = "sorry !Punished Procedure not deleted";
                lblDelete.ForeColor = Color.Red;
            }
        }

        protected void allTrainingGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            allTrainingGdv.PageIndex = e.NewPageIndex;
            GetTrainings();
        }

    }


}
